package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.Enterprise;
import com.jr.pojo.Users;
import com.jr.service.EnterpriseService;
import com.jr.service.SupperBlackService;
import com.jr.service.SupplierWhiteService;
import com.jr.service.UsersService;
import com.jr.service.impl.IEnterpriseServiceImpl;
import com.jr.service.impl.ISupperBlackServiceImpl;
import com.jr.service.impl.ISupperWhiteServiceImpl;
import com.jr.service.impl.IUserServiceImpl;
import com.jr.util.PageHelper;
import com.jr.vo.BlackEnter;
import com.jr.vo.WhiteEnter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Map;

@WebServlet("/blackenter/*")
public class BlackEnterController extends HttpServlet {
    UsersService us = new IUserServiceImpl();
    EnterpriseService eps = new IEnterpriseServiceImpl();
    private SupperBlackService sbs = new ISupperBlackServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // super.service(req, resp); //只根据【请求方式】分发；但是现在需要根据【请求路径】分发
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        Class cla = this.getClass();
        Method method = null;
        try {
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            method.invoke(this, request, response);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (
                InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    public void showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("分页查询controller+++++++++++++++++++++++++++");
        PageHelper ph = new PageHelper();
        String blackenter = request.getParameter("blackenter");
        //System.out.println("controller层拿到可选的企业名称了吗？ 分页查询用"+blackenter);
        String level = request.getParameter("level");
       // System.out.println("分页筛选条件：等级："+level);
        BlackEnter be = new BlackEnter();
        be.setEntername(blackenter);
        /*以上 ：如果进行条件分页 需要*/
       /* be.setScale(level);*/
        ph.setPageIndex(Integer.parseInt(request.getParameter("pageindex")));
        System.out.println("默认第一页："+ph.getPageIndex());
        /*需要用到当前账户的企业名称*/
        String account = request.getParameter("account");
        Users users = us.queryByAccount(account);
        Enterprise enter = eps.queryENterByName(users.getEntername());
      //拿到了 科技有限公司
        // 先查询总数据量
        //System.out.println("当前用户所在企业名称："+users.getEntername());
        ph.setDateCount(sbs.queryBlackCount(be, users.getEntername(),level));
       // System.out.println(ph + "ph分页对象sssssss");
        // 查询当前页数据
        /*这个enter是 */
        ArrayList<BlackEnter> blacklist = sbs.queryBlack(be,ph,users.getEntername(),level);
        ph.setListIndex(blacklist);
         System.out.println(ph + "controller层的ph对象");
       // System.out.println("phlistindex" + ph.getListIndex());
        //System.out.println("listIndex大小: " + (ph.getListIndex() != null ? ph.getListIndex().size() : "null"));
        //System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        response.getWriter().println(new Gson().toJson(ph));
    }


    /*jsp发请求 获得满足要求的企业名称*/
    public void queryEnterName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*直接获得sql语句的查询结果没有条件*/
        //eps.queryEnterprise()
        /*这块我给的企业名称是当前管理人员所在的企业名称*/
        String account = request.getParameter("account");
        Users users = us.queryByAccount(account);
        /*  System.out.println(users+"获得的对象");*/
        /*  System.out.println(users.getEntername()+"获得的对象的企业名称");//拿到了 科技有限公司*/
        /*根据当前登录的用户 我可以获得可以选择的企业名称*/
        //ArrayList list =  sws.queryWhiteEnter(users.getEntername());
        Map<Integer, String> integerStringMap = sbs.queryBlackEnter(users.getEntername());
        //ArrayList<String> valueList = new ArrayList<String>(integerStringMap.values());
        // System.out.println(valueList+"可选企业名字集合");
        response.getWriter().println(new Gson().toJson(integerStringMap));//直接传map集合
        //System.out.println("通过当前用户获得可选择企业名字"+list.get(0));
    }
    public void queryUnCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String entername = request.getParameter("selectedText");
        Enterprise enter = eps.queryENterByName(entername);
        /*获得对象 -- 获得对象的 统一社会代码*/
        String code = enter.getUniformcode();
        response.getWriter().println(new Gson().toJson(code));
    }




    public void addBlackEnter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        Users users1 = us.queryByAccount(account);
        users1.getEntername();//当前管理员所在企业
        Enterprise myenter = eps.queryENterByName(users1.getEntername());
        String addentername = request.getParameter("selectedText");//选择加入比啊名单的企业名称
        String reason = request.getParameter("reason");//拉黑的原因
        //System.out.println(reason+"**************拉黑原因***********");
        //System.out.println(addentername+"加入黑名单的企业名称");
        Enterprise addenter= eps.queryENterByName(addentername);
        addenter.getId();
        myenter.getId();
        Users users = us.queryByAccount(account);
        //String nowentername = users.getEntername();//当前对象的企业名称
        boolean b = sbs.addBlackEnter(myenter.getId(),  addenter.getId(), reason, LocalDate.now());

        response.getWriter().println(new Gson().toJson(b));
        showAll(request,response);
    }
    public void delBlackEnter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String blackname = request.getParameter("blackname");
        System.out.println("要删除的企业名字"+blackname);
        Enterprise whiteenter = eps.queryENterByName(blackname);
        String account = request.getParameter("account");
        Users users1 = us.queryByAccount(account);
        users1.getEntername();//当前管理员所在企业
        Enterprise myenter = eps.queryENterByName(users1.getEntername());
        Enterprise delenter = eps.queryENterByName(blackname);
        myenter.getId();//当前企业id
        delenter.getId();//删除企业id
        boolean del = sbs.dropBlack( myenter.getId(),delenter.getId());
        response.getWriter().println(new Gson().toJson(del));
    }
}
